package com.smec.moblie.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.smec.moblie.R;
import com.smec.moblie.bean.MaintenanceMemoBean;
import com.smec.moblie.common.ConstValue;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;

/**
 * 备忘列表adapter
 * Created by sunyubo on 16/2/29.
 */
public class MemoAdapter extends BaseAdapter implements ConstValue {

    private ArrayList<MaintenanceMemoBean> maintenanceMemoBeanList;
    private Context context;
    private LayoutInflater inflater;
    private int orderCode;

    public MemoAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        maintenanceMemoBeanList = new ArrayList<MaintenanceMemoBean>();
    }

    public void setList(ArrayList<MaintenanceMemoBean> maintenanceMemoBeanList, int orderCode) {
        this.maintenanceMemoBeanList = maintenanceMemoBeanList;
        this.orderCode = orderCode;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return maintenanceMemoBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return maintenanceMemoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = inflater.inflate(R.layout.item_remesmemo, null);

        EditText editText_memo = (EditText) convertView.findViewById(R.id.editText_memo);
        ImageView imageView_deleteMemo = (ImageView) convertView.findViewById(R.id.imageView_deleteMemo);

        editText_memo.setText(maintenanceMemoBeanList.get(position).getEle_memo_description());
        if (orderCode <= ORDER_ARRIVE || orderCode == ORDER_FINISH) {
            editText_memo.setEnabled(false);
        } else {
            editText_memo.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    maintenanceMemoBeanList.get(position).setEle_memo_description(s.toString());
                    maintenanceMemoBeanList.get(position).save();
                }
            });


            imageView_deleteMemo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null == maintenanceMemoBeanList.get(position).getEle_memo_description()) {
                        int ret = DataSupport.deleteAll(MaintenanceMemoBean.class, "ele_memo_id = ?", maintenanceMemoBeanList.get(position).getEle_memo_id());
                        if (ret > 0) {
                            maintenanceMemoBeanList.remove(position);
                        } else {
                            Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                        notifyDataSetChanged();
                    } else {
                        new AlertView("提示", "确定删除“" + maintenanceMemoBeanList.get(position).getEle_memo_description() + "”此条备忘？", "取消", new String[]{"确定"}, null, context,
                                AlertView.Style.Alert, new OnItemClickListener() {
                            @Override
                            public void onItemClick(Object o, int p) {
                                if (p == 0) {
                                    MaintenanceMemoBean maintenanceMemoBean = new MaintenanceMemoBean();
                                    maintenanceMemoBean.setMemo_status_code("COMPLETED");
                                    maintenanceMemoBean.update(maintenanceMemoBeanList.get(position).getId());
                                    maintenanceMemoBeanList.remove(position);
                                    notifyDataSetChanged();
                                }
                            }
                        }).show();
                    }
                }
            });
        }
        return convertView;
    }
}
